<?php

// phpMyRealty 3
//
// File Name: pdf.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

define('PMR', 'true');

include('./config.php');
include ( PATH . '/defaults.php');
include ( PATH . '/includes/ufpdf.php' );

error_reporting  ( 'E_NONE' );

if (isset($_GET['id']) && eregi('^[0-9]+$', $_GET['id']))
     
 {

  class PDF extends UFPDF
   {

   //Page footer
   function Footer()
    {

     global $conf;

     //Position at 1.5 cm from bottom
     $this->SetY(-15);
     //Arial italic 8
     $this->SetFont('Times','I',8);
     //Page number
     $this->Cell(10, 0, $conf['website_name_short']  . ' / Page ' . $this->PageNo() . '' ,0,0,'R');
    }

   }

  $pdf=new PDF();

  $pdf->AddFont('FreeSans', '', PATH . '/includes/fpdf_font/FreeSans.php');
  $pdf->AddFont('FreeSans', 'B', PATH . '/includes/fpdf_font/FreeSansBold.php');

  define('EURO',chr(128));

  $pdf->AliasNbPages();

  $pdf->SetAutoPageBreak('auto');
  $pdf->AddPage();
  
	// Title/descr language to use (if available)
	$title = str_replace( 'name', 'title', $language_in );
	$description = str_replace( 'name', 'description', $language_in );

  $sql = 'SELECT ' . $title . ', ' . $description . ', ' . PROPERTIES_TABLE . '.* FROM ' . PROPERTIES_TABLE  . ' WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  $r = $db->query ( $sql ) or error ('Critical Error' , mysql_error());

  $f = $db->fetcharray( $r );

  // Make all values in the array 'n/a' if empty
  $f = array_map ( 'if_empty', $f);

  $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE approved = 1 AND id = "' . $f['userid'] . '" LIMIT 1';
  $r_user = $db->query ( $sql ) or error ('Critical Error' , mysql_error());
  $f_user = $db->fetcharray ($r_user);

  if ($f_user['package'] != '0' && $f_user['package'] != '')
   {

    $sql = 'SELECT * FROM ' . PACKAGES_AGENT_TABLE  . ' WHERE id = "' . $f_user['package'] . '" LIMIT 1';
    $r_package = $db->query ( $sql );
    $f_package = $db->fetcharray ( $r_package );

   }

  else

   {

    $f_package['listings'] = $conf['free_listings'];
    $f_package['gallery'] = $conf['free_gallery'];
    $f_package['mainimage'] = $conf['free_mainimage'];
    $f_package['photo'] = $conf['free_photo'];
    $f_package['phone'] = $conf['free_phone'];
    $f_package['address'] = $conf['free_address'];
    
   }

  $pdf->setXY(150, 25);
  
	// Default
	if ($f[0] == '')
		$f['title'] = $f['title'];
	else
		$f['title'] = $f[0];
		
	if ($f[1] == '')
		$f['description'] = $f['description'];
	else
		$f['description'] = $f[1];
  
  $data = $lang['View_Realtor'] . ":\n";
  
  	// If they have an account
  	if (auth_check($session->fetch('login'), $session->fetch('password')))
  	{
		  // Fetching the user ID from the user's table
		  $sql = 'SELECT approved, id, package FROM ' . USERS_TABLE . ' WHERE login = "' . $session->fetch('login') . '" LIMIT 1';
		  $r = $db->query( $sql );
		  $f2 = $db->fetcharray( $r );
		  $db->freeresult($r);
		  
		  $account = true;
		  
		  if ($f2['package'] > 0)
		  	$paid_account = true;
		  else
		  	$paid_account = false;
  	}
  	else
  	{
  		$paid_account = false;
  		$account = false;
  	}
  
	// Account only, but they don't have an account
	if ($conf['contact_agents'] == '2' && $account == false)
	{
		$disable = true;
		$agent_details = $lang['Agent_Details_Account_Only'];
	}
	// Paid account only, but they don't have a paid account
	elseif ($conf['contact_agents'] == '3' && $paid_account == false)
	{
		$disable = true;
		$agent_details = $lang['Agent_Details_Pay_Only'];
	}
	
	if ($disable == true)
	{
		$data .= $agent_details . "\n";
	}
		else
	{

  		$data .= $f_user['first_name'] . ' ' . $f_user['last_name'] . "\n";
  		$data .= $f_user['company_name'] . "\n";

		  if ($f_package['phone'] == 'ON')
		   {
		
		    $data .= $lang['Realtor_Phone'] . ': ' . $f_user['phone'] . "\n";
		    $data .= $lang['Realtor_Fax'] . ': ' . $f_user['fax'] . "\n";
		    $data .= $lang['Realtor_Mobile'] . ': ' . $f_user['mobile'] . "\n";
		   }

	}

  $pdf->SetFont('FreeSans','B',8);

  $pdf->MultiCell(50, 5, unsafehtml_pdf($data), '1', 'R');

  if ($f_package['mainimage'] == 'ON' && file_exists( PATH . '/images/' . $f['id'] . '.jpg' )) {
   $pdf->Image( PATH . '/images/' . $_GET['id'] . '.jpg', 10, 10, 50);
   $start = 50;
  }

  $sql = 'SELECT * FROM ' . GALLERY_TABLE . ' WHERE listingid = "' . $f['id'] . '" ORDER BY id';
  $r_gallery = $db->query( $sql ) or error ('Critical Error', mysql_error ());

  if ($db->numrows($r_gallery) > 0 && $start != 50) {
   $start = 10;

   while ($f_gallery = $db->fetcharray($r_gallery))
    {
     $pdf->Image( PATH . '/gallery/' . $f_gallery['id'] . '.jpg', 10, $start, 50);
     $start = $start + 40;
    }
  }
  elseif ($db->numrows($r_gallery) > 0 && $start == 50) {
   $start = 60;

   while ($f_gallery = $db->fetcharray($r_gallery))
    {
     $pdf->Image( PATH . '/gallery/' . $f_gallery['id'] . '.jpg', 10, $start, 50);
     $start = $start + 40;
    }
  }

  // 
  
  $pdf->SetFont('FreeSans','B',10);
  $pdf->setXY(65, 10);

  $pdf->MultiCell(120, 5, unsafehtml_pdf($f['title']) . "\n\n" . unsafehtml_pdf($conf['currency']) . ' ' . pmr_number_format($f['price']) );

  $pdf->SetFont('FreeSans','',8);

  $pdf->setXY(65, 30);

  $data = '';

  if ($f['display_address'] == 'YES' &&  $f_package['address'] == 'ON')

   {

    $data .= $lang['Realtor_Address'] . ': ' . "\n";

    $cat = explode ("#", $f['location2']);
    $r11 = $db->query('SELECT category FROM ' . LOCATION1_TABLE . ' WHERE selector = "' . $cat[0] . '"');
    $f11 = $db->fetcharray($r11);
    $location1 = $f11['category'];

    $r22 = $db->query('SELECT subcategory FROM ' . LOCATION2_TABLE . ' WHERE catsubsel = "' . $cat[1] . '"');
    $f22 = $db->fetcharray($r22);
    $location2 = $f22['subcategory'];

    $r33 = $db->query('SELECT subsubcategory FROM ' . LOCATION3_TABLE . ' WHERE catsubsubsel = "' . $cat[2] . '"');
    $f33 = $db->fetcharray($r33);
    $location3 = $f33['subsubcategory'];

    $data .= $f['address1'] . ' ' . $f['address2'] . ", \n" . $f['zip'] . ' ' . $location3 . ' ' . $location2 . ' ' . $location1 . "\n\n";

   }

  $data .= $lang['Listing_Bedrooms'] . ': ' . $f['bedrooms'] . "\n";
  $data .= $lang['Listing_Bathrooms'] . ': ' . $f['bathrooms'] . "\n";
  $data .= $lang['Listing_Half_Bathrooms'] . ': ' . $f['half_bathrooms'] . "\n\n";

  $data .= str_replace("<br>", "", removehtml(unsafehtml_pdf($f['description']))) . "\n\n";

  $data .= $lang['Listing_Status'] . ': ' . getnamebyid( STATUS_TABLE, $f['status'] ) . "\n";
  if (strcasecmp($conf['show_mls'], 'OFF')) {
    $data .= $lang['Listing_MLS'] . ': ' . $f['mls'] . "\n";
  }
  $data .= $lang['Module_Listing_Type'] . ': ' . getnamebyid ( TYPES2_TABLE, $f['type2'] ) . "\n";
  $data .= $lang['Listing_Property_Type'] . ': ' . getnamebyid ( TYPES_TABLE, $f['type'] ) . "\n";
  $data .= $lang['Listing_Style'] . ': ' . getnamebyid ( STYLES_TABLE, $f['style'] ) . "\n";
  $data .= $lang['Listing_Lot_Size'] . ': ' . $f['size'] . "\n";
  $data .= $lang['Listing_Dimensions'] . ': ' . $f['dimensions'] . "\n";
  $data .= $lang['Listing_Year_Built'] . ': ' . $f['year_built'] . "\n";

  $data .= $lang['Listing_Garage'] . ': ' . getnamebyid ( GARAGE_TABLE, $f['garage'] ) . "\n";
  $data .= $lang['Listing_Garage_Cars'] . ': ' . $f['garage_cars'] . "\n";
  $data .= $lang['Listing_Basement'] . ': ' . getnamebyid ( BASEMENT_TABLE, $f['basement'] ) . "\n\n";

  $data .= $lang['Listing_Additional_Out_Buildings'] . ":\n" . str_replace("<br>", "\n", show_multiple ( BUILDINGS_TABLE, $f['buildings'] )) . "\n";
  $data .= $lang['Listing_Appliances_Included'] . ":\n" . str_replace("<br>", "\n", show_multiple ( APPLIANCES_TABLE, $f['appliances'] )) . "\n";
  $data .= $lang['Listing_Features'] . ":\n" . str_replace("<br>", "\n", show_multiple ( FEATURES_TABLE, $f['features'] )) . "\n";

  $data .= $lang['Date_Added'] . ': ' . printdate($f['date_added']) . "\n";
  $data .= $lang['Date_Updated'] . ': ' . printdate($f['date_updated']) . "\n\n";
  
	// Custom fields
	
	// Check if custom field is input, if it is, it will have {INPUT}{value} in the database, otherwise, it will have a primary key reference
	$custom_fields = array(
						'custom1' => $f['custom1'],
						'custom2' => $f['custom2'],
						'custom3' => $f['custom3'],
						'custom4' => $f['custom4'],
						'custom5' => $f['custom5'],
						'custom6' => $f['custom6'],
						'custom7' => $f['custom7'],
						'custom8' => $f['custom8'],
						'custom9' => $f['custom9'],
						'custom10' => $f['custom10'],
	);
	foreach ( $custom_fields AS $key => $value )
	{
		if ( strpos( $value, '{INPUT}' ) !== false )
		{
			// Input box
			$f[$key] = str_replace( '{INPUT}', '', $value );
		}
		else
		{
			// Select box
			$f[$key] = show_custom_value ( $db, $f[$key],  VALUES_TABLE );
		}
		
		if ( $f[$key] != '' )
		{
			// Add to PDF output
			$data .= show_custom_value( $db, $key, FIELDS_TABLE ) . ': ' . $f[$key] . "\n";
		}
	}

  $pdf->MultiCell(120, 5, unsafehtml_pdf($data));

  $pdf->Output();

 }

else

 echo 'Sorry, ID is not specified';

?>